<?php
/**
 * Created by PhpStorm.
 * User: bxm
 * Date: 2022/6/29
 * Time: 16:41
 */

namespace app\api\controller\worker;

use app\api\controller\Base;
use app\common\model\node\NodeProcessModel;
use app\common\model\sale\SaleAfterDetailModel;
use app\common\model\sale\SaleAfterModel;
use app\common\model\worker\CustomerModel;
use app\common\model\worker\TaskModel;
use think\facade\Db;
use think\Request;

class SaleAfterDetailController extends Base
{
    private $model;
    private $taskModel;
    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {

        parent::__construct();
        $this->checkLogin();
        $this->model = new SaleAfterDetailModel();
        $this->taskModel = new TaskModel();

    }
    public function list(){
        $param = $this->request->param();
        $pageSize      = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d') ?? 1; //当前页

        $list = $this->model->alias('cal')
//            ->leftJoin('sale_after sa', 'sa.id = cal.sale_id')
            ->leftJoin('admin a', 'a.id = cal.action_admin_id')
            ->where('cal.customer_id',$param['customer_id'])
            ->order('cal.id','desc')
            ->field('a.nickname,cal.*')
             ->paginate(['list_rows'=> $pageSize, 'page' => $pageNumber]);

        $this->result($list,'操作成功',1);


    }
    public function add(){
        $param = $this->request->param();
        $rule = [
            'customer_id|客户id'=>'require',
            'sale_id|售后id'=>'require',
            'result_imgs|收获处理结果'=>'require',
            'voucher_imgs|收款凭证'=>'require',
        ];
        $this->validate($param,$rule);
        $data = [
            'customer_id'=>$param['customer_id'],
            'sale_id'=>$param['sale_id'],
            'result_imgs' => json_encode($param['result_imgs'],JSON_UNESCAPED_UNICODE),
            'voucher_imgs' => json_encode($param['voucher_imgs'],JSON_UNESCAPED_UNICODE),
        ];
        $data['action_admin_id'] = $this->loginInfo['id'];
        // 启动事务
        Db::startTrans();
        $info = $this->model->add($data);
        if(!$info){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        $saleInfo = SaleAfterModel::where('id',$param['sale_id'])->find();
        $nodeProcess = NodeProcessModel::where('node_id', 15)->order('id', 'asc')->find();
        $dataTask = [
            'customer_id' => $data['customer_id'],
            'order_id' => 0,
            'node_id' => 15,
            'process_id' => $nodeProcess['id'],
            'receive_admin_id' => $saleInfo['action_admin_id'],
            'action_table_id'=>$info['id'],//操作表id
            'admin_id'=>$this->loginInfo['id']
        ];

        $result = $this->taskModel->commonAddTask($dataTask);
        if(!$result){
            // 回滚事务
            Db::rollback();
            $this->result([], "操作失败", 0);
        }
        Db::commit();
        $this->taskModel->sendTaskInfo($dataTask['receive_admin_id'],$nodeProcess['process_name']);
        $this->result($info,'操作成功',1);
    }

    /**
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author bxm
     * @date 2022/8/1 17:49
     */
    public function getInfo(Request $request)
    {
        $param = $this->request->param();
        $rule = [
            'customer_id|客户id'=>'require',
        ];
        $data = [];
        $customerInfo = CustomerModel::where('id',$param['customer_id'])->find();
        $data['customer_info'] = $customerInfo;
        $saleAfterInfo = SaleAfterModel::alias('as')->leftJoin('admin a', 'a.id = as.sale_admin_id')
            ->where('as.customer_id',$param['customer_id'])->field('a.nickname,as.*')->order('as.id','desc')->find();
        $saleAfterInfo['sale_imgs'] = json_decode($saleAfterInfo['sale_imgs'],true);
        $data['sale_info'] =$saleAfterInfo;
        $this->validate($param,$rule);


        $this->result($data,'操作成功',1);
    }

    public function getLogInfo(Request $request){
        $param = $request->param();
        //开始校验
        $this->validate($param, [
            'id|记录id' => 'require',
        ]);
        $id = $param['id'];
        $list = $this->model->alias('cal')
            ->leftJoin('sale_after sa', 'sa.id = cal.sale_id')
            ->leftJoin('admin a', 'a.id = sa.sale_admin_id')
            ->where('cal.id', $id)
            ->field('a.nickname,cal.*,sa.sale_name,sa.sale_moblie,sa.sale_time,sa.is_make,sa.make_time,sa.is_pay,sa.amount,sa.sale_imgs,
            sa.sale_admin_id,sa.is_pay_wage,sa.wage_amount,sa.pay_remark')
            ->find();
        $this->result($list, '操作成功', 1);
    }

}